﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace X3DXNA.utils
{
    public static class TriangleRenderer
    {
        static BasicEffect lineEffect;

        // Custom rasterizer state for drawing in wireframe.
        static RasterizerState WireFrame = new RasterizerState
        {
            FillMode = FillMode.WireFrame,
            CullMode = CullMode.None
        };

        public static void DrawPickedTriangle(Vector3[] triangle, GraphicsDevice graphicsDevice, Matrix view, Matrix projection, Color color)
        {
            if (lineEffect == null)
            {
                lineEffect = new BasicEffect(graphicsDevice);
            }

            VertexPositionColor[] pickedTriangle =
            {
                new VertexPositionColor(triangle[0], color),
                new VertexPositionColor(triangle[1], color),
                new VertexPositionColor(triangle[2], color),
            };

            // Set line drawing renderstates. We disable backface culling
            // and turn off the depth buffer because we want to be able to
            // see the picked triangle outline regardless of which way it is
            // facing, and even if there is other geometry in front of it.
            graphicsDevice.RasterizerState = WireFrame;
            graphicsDevice.DepthStencilState = DepthStencilState.None;

            // Activate the line drawing BasicEffect.
            lineEffect.Projection = projection;
            lineEffect.View = view;

            lineEffect.CurrentTechnique.Passes[0].Apply();

            // Draw the triangle.
            graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList,
                                              pickedTriangle, 0, 1);

            graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
        }
    }
}
